var PATH_PREFIX ="";
var prdCategoryArr = [];
var prjTermArr = [];
var prjprcstatArr = [];

function setPathPrefix(path){
	PATH_PREFIX =  path;
}

$(function(){
	// 给select2组件加载样式
	WASP_WIDGET.initializeSelectStyle();
	// 注册清空事件
    WASP_WIDGET.registerResetClearEvent();
    //初始化日期控件
    WASP_WIDGET.triggerDateRangeStyle("q-setupDate");
    WASP_WIDGET.triggerDateRangeStyle("q-expectedEndDate");
 // 是否选中(样式切换)
    initChoose('prdCategory');
    initChoose('prjTerm');
    initChoose('prjprcstat');
    productManagerList();
});

var $prdList = $('#projectManagerList');
function productManagerList() {
	$prdList.jqGrid({   
		url:PATH_PREFIX+'projectManagerList.do',
		datatype: "json",
		colNames:["产品编码","产品简称","产品分类","产品状态",/*"产品期限",*/"运作模式","业务类型","募集方式","开放频率","成立日","到期日"],
		colModel:[  { name: 'prjcode', hidden : true,index: 'prjcode', key: true, width: 30, resizable: true, sortable: false },     
		            { name: 'prjsname', index: 'prjsname', width: 30, resizable: true, sortable: false,formatter:showTitle},
		            { name: 'prdCategoryNM', index: 'prdCategory', width: 30, resizable: true, sortable: false },
		            { name: 'prjprcstatNM', index: 'prjprcstat', width: 30, resizable: true, sortable: false },     
		            /*{ name: 'prjTerm', index: 'prjTerm', width: 30, resizable: true, sortable: false },*/
		            { name: 'operationModeNM', index: 'operationMode', width: 30, resizable: true, sortable: false },
		            { name: 'prdBizTypeNM', index: 'prdBizType', width: 30, resizable: true, sortable: false },
		            { name: 'collectionMethodNM', index: 'collectionMethod', width: 30, resizable: true, sortable: false },
		            { name: 'openFrequencyNM', index: 'openFrequency', width: 30, resizable: true, sortable: false },
		            { name: 'setupDate', index: 'setupDate', width: 30, resizable: true, sortable: false,formatter:dateFormatter},
		            { name: 'expectedEndDate', index: 'expectedEndDate', width: 30, resizable: true, sortable: false,formatter:dateFormatter}/*,
		            { name: 'option', index: 'option', width: 20, resizable: true, sortable: false }*/
				  ],
		rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 50,
        prmNames: { search: "search", page: "pageNo", rows: "limit" },
        height: 'auto',
        width: false,
        autowidth:true,
        editurl: '',
        viewrecords: true,
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },  
		multiselect:false,
		pager: "#projectManagerListPage",					
		viewrecords: true,
		hidegrid: false, 
		gridComplete: function(){
			var ids = $prdList.jqGrid('getDataIDs');
			for(var i=0;i < ids.length;i++){
				var rowId = ids[i];
				var params = {
						'detail' : 'productDetailTemple'
					};
				var permissionButton = Permission_Button.configPermissionButton(params);
				permissionButton.detail = "查看";
				/*console.log(permissionButton.prjsname);*/
                $prdList.jqGrid('setRowData', ids[i], { option: Permission_Button.linkAllBtn(permissionButton)});
			}	
		}
	});
	$prdList.navGrid('#projectManagerListPage',{edit:false,add:false,del:false,search:false,refreshstate:'current'});
	$prdList.jqGrid('setFrozenColumns');
	jqGridResize($prdList);
};

//清空 选中
function clearCondtion(){
	$('.fa-check-square').addClass('fa-square');
	$('.fa-check-square').removeClass('fa-check-square');
}

//是否选中(样式切换)
function initChoose(att){
    $('.'+att).on('click', function () {
    	var attr = $(this).attr("class");
    	if(attr.indexOf("fa-square") > -1){
    		$(this).removeClass('fa-square');
    		$(this).addClass('fa-check-square');
		}else{
			$(this).removeClass('fa-check-square');
    		$(this).addClass('fa-square');
		}
	});
}

function showTitle(text, options, rData){
	var prjcode = rData["prjcode"];
	if(prjcode==null || prjcode == ''){
		return "";
	}
	return "<a href=javascript:showProductContent('"+ prjcode +"') class='showunderline'>"+text+"</a>";
}

function dateFormatter(text, options, rData){
	if(text == null){
		return "";
	}
	return moment(text).format('YYYY-MM-DD');
}

//查看单产品详情信息
function showProductContent(prjcode){
	var actionUrl = PATH_PREFIX + "singleProductOverview.do?prjcode="+prjcode;
	window.open(actionUrl);
}

//系列产品新增
function projectAdd(){
	var actionUrl = PATH_PREFIX + "projectAdd.do";
	window.open(actionUrl);
}

//产品新增
function productAdd(){
	var actionUrl = PATH_PREFIX + "productAdd.do";
	window.open(actionUrl);
}

//子产品新增
function subProductAdd(){
	var actionUrl = PATH_PREFIX + "subProductAdd.do";
	window.open(actionUrl);
}

//产品管理删除操作
function productManagerDel(){
	var actionUrl = PATH_PREFIX + "productManagerDel.do";
	window.open(actionUrl);
}
//产品状态修改
function productManagerUpd(){
	var actionUrl = PATH_PREFIX + "productManagerUpd.do";
	window.open(actionUrl);
}

//产品导出
function exportProductDownload(){
	var prjsname = $.trim($("#q-prjsname").val());
	var prdCategory = getChosen('prdCategory');
	var prjprcstat = getChosen('prjprcstat');
	var prjTerm = getChosen('prjTerm');
	var startSetupDate    = $("#q-setupDate").getDateRangeValue()[0]; 
	var endSetupDate    = $("#q-setupDate").getDateRangeValue()[1]; 
	var startExpectedEndDate    = $("#q-expectedEndDate").getDateRangeValue()[0]; 
	var endExpectedEndDate    = $("#q-expectedEndDate").getDateRangeValue()[1]; 
	var postData = $prdList.jqGrid("getGridParam", "postData");
    var urlParam ='sp[prjsname]='+prjsname
	+'&sp[prdCategory]='+prdCategory
	+'&sp[prjTerm]='+prjTerm
	+'&sp[prjprcstat]='+prjprcstat
	+'&sp[cTtartTime]='+startSetupDate
	+'&sp[endSetupDate]='+endSetupDate
	+'&sp[startExpectedEndDate]='+startExpectedEndDate
	+'&sp[endExpectedEndDate]='+endExpectedEndDate;
    debugger;
    var actionUrl = PATH_PREFIX + "productDataExport.do?"+encodeURI(urlParam);
    window.open(actionUrl);
}

/*
 * 条件查询
 */
function queryByCondtion(prjprcstat){
	var prjsname = $.trim($("#q-prjsname").val());
	var prdCategory = getChosen('prdCategory');
	var prjTerm = getChosen('prjTerm');
	if("" == prjprcstat || undefined == prjprcstat){
		prjprcstat = getChosen('prjprcstat');
	}else{
		$($(".fa-square.prjprcstat")[prjprcstat-1]).removeClass('fa-square');
		$($(".prjprcstat")[prjprcstat-1]).addClass('fa-check-square');
	}
	var startSetupDate    = $("#q-setupDate").getDateRangeValue()[0]; 
	var endSetupDate    = $("#q-setupDate").getDateRangeValue()[1]; 
	var startExpectedEndDate    = $("#q-expectedEndDate").getDateRangeValue()[0]; 
	var endExpectedEndDate    = $("#q-expectedEndDate").getDateRangeValue()[1]; 
	var postData = $prdList.jqGrid("getGridParam", "postData");
	$.extend(postData,{
    	'sp[prjsname]':prjsname,
    	'sp[prdCategory]':prdCategory,
    	'sp[prjprcstat]':prjprcstat,
    	'sp[prjTerm]':prjTerm,
    	'sp[startSetupDate]':startSetupDate,
    	'sp[endSetupDate]':endSetupDate,
    	'sp[startExpectedEndDate]':startExpectedEndDate,
    	'sp[endExpectedEndDate]':endExpectedEndDate,
    	'sp[postData]':postData
    });
	$prdList.trigger("reloadGrid",[{page:1}]);  //重新载入Grid表格
}

//获取选中的值
function getChosen(element){
	var data = '';
	//遍历
    $('.'+element+'.fa-check-square').each(function(index,element){
    	data += $(element).attr("value")+',';
    });
    if(data.length > 0){
    	data = data.substr(0,data.length-1);
    }
    return data;
}

//绑定回车事件,查询
$("body").keypress(function(event){
	var key = event.which;
	if(key == "13"){
		queryByCondtion(true);
	}
});